$(function () {
    obj.init();
});

var obj = new function () {
    return {
        dataGrid: null,
        init: function () {
            this.dataGrid = new xhDataGrid($('#list'), 'device/getBoxInfoData');
            this.dataGrid.parseToolbar().init().activeButton(['bt_list_del']);
            xhExtra.clearLoadSuccess(this.dataGrid);
            this.listener();
        },
        listener: function () {
            var $this = this;
            //查询
            xhTool.bindDocKeyDown(
                $('#bt_list_search').click(function () {
                    $this.funObj().searchList();
                })
            );
            //双击修改
            $this.dataGrid.onDblClickRow = function(rowIndex, rowData){
                var id = rowData['iId'];
                if (!id) {
                    window.top.xhTop.showError('缺少记录id，不可操作');
                    return false;
                }
                $this.funObj().modifyBoxInfo(id);
            };
            //增加
            $('#bt_list_add').click(function () {
                $this.funObj().addBoxInfo();
            });
            //删除
            $('#bt_list_del').click(function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().delBoxInfo();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                searchList: function () {
                    $this.dataGrid.reload({
                        search: {
                            vague: xhExtra.getValue($('#list_vague'))
                        },
                        status: xhExtra.getValue($('#box_info_status')),
                        boxType: xhExtra.getValue($('#box_info_boxType')),
                        schoolType: xhExtra.getValue($('#box_info_schoolType')),
                        schoolStatus: xhExtra.getValue($('#box_info_schoolStatus')),
                        macStatus: xhExtra.getValue($('#box_info_macStatus'))
                    })
                },
                addBoxInfo: function () {
                    window.top.xhTop.showModal({
                        title: '新增盒子资料',
                        content: xhTool.getFrameString('device/addBoxInfo', {}, 430),
                        size: 'md',
                        width: 800,
                        backClose: false,
                        button: '确认'
                    });
                },
                modifyBoxInfo: function (_id) {
                    window.top.xhTop.showModal({
                        title: '修改盒子资料',
                        content: xhTool.getFrameString('device/addBoxInfo', {id: _id}, 430),
                        size: 'md',
                        width: 800,
                        backClose: false,
                        button: '确认'
                    });
                },
                delBoxInfo: function () {
                    var id = $this.dataGrid.getSelectValues('iId');
                    if (id.length < 1) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    xhExtra.askChangeStatus('device/delBoxInfo', {id: id}, $this.dataGrid, '确认删除？');
                }
            }
        }
    }
};